home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tclm_1_0.lha / tclm-1.0 / main.c < prev    next >
C/C++ Source or Header  |  1993-08-16  |  4KB  |  138 lines

  1. /*-
  2.  * Copyright (c) 1993 Michael B. Durian.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 1. Redistributions of source code must retain the above copyright
  8.  *    notice, this list of conditions and the following disclaimer.
  9.  * 2. Redistributions in binary form must reproduce the above copyright
  10.  *    notice, this list of conditions and the following disclaimer in the
  11.  *    documentation and/or other materials provided with the distribution.
  12.  * 3. All advertising materials mentioning features or use of this software
  13.  *    must display the following acknowledgement:
  14.  *    This product includes software developed by Michael B. Durian.
  15.  * 4. The name of the the Author may be used to endorse or promote 
  16.  *    products derived from this software without specific prior written 
  17.  *    permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
  22.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  23.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29.  * SUCH DAMAGE.
  30.  */
  31. /*
  32.  * main.c,v 1.4 1993/05/07 17:45:03 durian Exp
  33.  */
  34.  
  35. static char cvsid[] = "main.c,v 1.4 1993/05/07 17:45:03 durian Exp";
  36.  
  37. #include "tclInt.h"
  38. #include "mutil.h"
  39. #include "tclm.h"
  40.  
  41. /*
  42.  * Declarations for library procedures:
  43.  */
  44.  
  45. extern int isatty();
  46.  
  47. Tcl_Interp *interp;
  48. Tcl_CmdBuf buffer;
  49. int tty;
  50.  
  51. void usage _ANSI_ARGS_(());
  52. extern char *optarg;
  53.  
  54.     /* ARGSUSED */
  55. int
  56. main(argc, argv)
  57.     int argc;
  58.     char **argv;
  59. {
  60.     static int gotPartial = 0;
  61.     int result;
  62.     FILE *file;
  63.     char *args;
  64.     char *cmd;
  65.     int have_f;
  66.     char buf[20];
  67.     char line[200];
  68.     char opt;
  69.  
  70.     have_f = 0;
  71.     file = stdin;
  72.  
  73.     /*
  74.      * we want to stop parsing args when we get a -f, so the
  75.      * script can get the args it wants
  76.      */
  77.     while (!have_f && (opt = getopt(argc, argv, "f:")) != -1) {
  78.         switch (opt) {
  79.         case 'f':
  80.             if ((file = fopen(optarg, "r")) == NULL) {
  81.                 fprintf(stderr, "Couldn't open %s\n", optarg);
  82.                 exit(1);
  83.             }
  84.             have_f = 1;
  85.             break;
  86.         case '?':
  87.             usage();
  88.             exit(1);
  89.         }
  90.     }
  91.  
  92.     interp = Tcl_CreateInterp();
  93.     args = Tcl_Merge(argc-1, argv+1);
  94.     Tcl_SetVar(interp, "argv", args, TCL_GLOBAL_ONLY);
  95.     ckfree(args);
  96.     sprintf(buf, "%d", argc - 1);
  97.     Tcl_SetVar(interp, "argc", buf, TCL_GLOBAL_ONLY);
  98.     Tclm_InitMidi(interp);
  99.     buffer = Tcl_CreateCmdBuf();
  100.     tty = isatty(0);
  101.  
  102.     for (;;) {
  103.         if (file == stdin && tty)
  104.             printf("tclm: ");
  105.  
  106.         if (fgets(line, 200, file) == NULL)
  107.             break;
  108.         cmd = Tcl_AssembleCmd(buffer, line);
  109.         if (cmd == NULL) {
  110.             gotPartial = 1;
  111.             continue;
  112.         }
  113.         gotPartial = 0;
  114.         result = Tcl_RecordAndEval(interp, cmd, 0);
  115.         if (*interp->result != 0) {
  116.             if (result != TCL_OK) {
  117.                 printf("%s\n", Tcl_GetVar(interp, "errorInfo",
  118.                     0));
  119.                 if (file !=stdin || !tty)
  120.                     break;
  121.             } else if (file == stdin && tty) {
  122.                 printf("%s\n", interp->result);
  123.             }
  124.         }
  125.     }
  126.  
  127.     Tcl_DeleteInterp(interp);
  128.     Tcl_DeleteCmdBuf(buffer);
  129.     exit(0);
  130. }
  131.  
  132. void
  133. usage()
  134. {
  135.  
  136.     (void) fprintf(stderr, "Usage: tclm [ -f filename ]\n");
  137. }
  138.